/**
 * 生产片
 */
'use strict'
const schedule = require('node-schedule');
const flawTypeArr = require('./flaw_type');
const { piantieqian_db_config } = require('./config.json').station_db; // 源库
const seq = require('./lib/db').getSequelizeInstance(piantieqian_db_config);
const { initPanelInfoData_1, Panel_info_1 } = require('./model/1/panel_info')
const DATE = require('./date')
const A = require('./class/1');
const B = require('./class/2');
const C = require('./class/3');
const D = require('./class/4');
const E = require('./class/5');
const F = require('./class/6');
class Main {
    constructor() {
        initPanelInfoData_1(seq)
        // 片的数组
        this.panelArr = [];
        this.flawArry = [];
        this.index = 0;
        this.head_md = 0;
        this.head_cd = 0;
        this.doffSum = 0;
        this.cuurntDoff = 0;
        this.PanelALllInfoArr = [];
        this.day = 1;
        this.hours = 4;
        this.minites = 0;
        this.seconds = 0;
        this.time = DATE;
        this.time_index = 0;
        this.timmerForIntaravl = 1;
        // 5s生产一个片
        this.startProduction();
    }
    async getAsyncMaxPanelUid() {
        try {
            let res = await Panel_info_1.getMaxUid(seq);
            console.log(res);
            this.index = res;
        } catch (_) {
            console.log(_);
        }
    }

    async startProduction() {
        await this.getAsyncMaxPanelUid();
        // 创建工单
        // this.createJobInfo();
        // 创建片 3s中创建一次
        this.job = schedule.scheduleJob(`*/5 * * * * *`, async () => {
            const panel = this.createPanel();
            this.panelArr.push(panel);
            // 推到第一站上
            this.pushLine(panel);
        })
    }

    createPanel() {
        // 随机产生重复的片
        //生成片
        const cd = Math.random() * 20 + 8192;
        const md = 28788 + Math.random() * 30;
        const panel = `PANELID0000${++this.index}`;
        const num = Math.random() * 10;
        let flawCount = 0;
        if (num > 5) {
            flawCount = Math.floor(Math.random() * 15) + 5;
        }
        if (this.time >= Date.now() || this.time_index == 1) {
            this.job.reschedule(`*/15 * * * * *`);
            this.time = Date.now();
            this.time_index = 1;
        } else {
            if (this.index % 1920 == 0) {
                this.day++;
                this.seconds = 0;
                this.minites = 0;
                this.hours = 4;
            }
            this.time = new Date(`2021-9-${this.day} ${this.hours % 60}:${this.minites % 60}:${this.seconds % 60}`).getTime();
            this.seconds += 15;
            this.minites = Math.floor(this.seconds / 60)
            this.hours = Math.floor(this.minites / 60) + 4
        }
        console.log(this.time);
        console.log(this.formatDate(this.time));
        const panelInfo = {
            uid: this.index,
            panelId: panel,
            flawCount: flawCount,
            startTime: this.time,
            finish_time: this.time,
            exConfig1: `{"job": {"modelSize": { "value": "${cd};${md}" } }}`,
            length_md: md,
            length_cd: cd,
            head_md: this.head_md,
            head_cd: this.head_cd
        };
        console.log(`***当前片数${this.index}***当前片ID${panel}***`);
        this.head_md = md + this.head_md;
        this.head_cd = cd + Math.random() * 5;
        this.PanelALllInfoArr.push({
            panel: panelInfo,
        })
        return panelInfo;
    }
    /**
    * @param {时间戳} date 
    * @returns 格式化后的时间
    */
    formatDate(date) {
        let time = new Date(date);
        let minites = time.getMinutes() < 10 ? `0${time.getMinutes()}` : time.getMinutes();
        let seconds = time.getSeconds() < 10 ? `0${time.getSeconds()}` : time.getSeconds();
        return `${time.toLocaleDateString()} ${time.getHours()}:${minites}:${seconds}`;
    }
    pushLine(panel) {
        // 1
        let timmer1 = setTimeout(() => {
            a++;
            A.createPanelInfo(this.PanelALllInfoArr[a].panel, this.time);
            clearTimeout(timmer1);
        }, 2000)
        // 2
        let timmer2 = setTimeout(() => {
            b++;
            B.createPanelInfo(this.PanelALllInfoArr[b].panel, this.time);
            clearTimeout(timmer2);
        }, 4000)
        // 3
        let timmer3 = setTimeout(() => {
            c++;
            C.createPanelInfo(this.PanelALllInfoArr[c].panel, this.time);
            clearTimeout(timmer3);
        }, 6000)
        // 4
        let timmer4 = setTimeout(() => {
            d++;
            D.createPanelInfo(this.PanelALllInfoArr[d].panel, this.time);
            clearTimeout(timmer4);
        }, 8000)
        // 5
        let timmer5 = setTimeout(() => {
            e++;
            E.createPanelInfo(this.PanelALllInfoArr[e].panel, this.time);
            clearTimeout(timmer5);
        }, 10000)
        // 6
        let timmer6 = setTimeout(() => {
            f++;
            F.createPanelInfo(this.PanelALllInfoArr[f].panel, this.time);
            // 当第六站有数据之后，在第六站的所有片中抽取 20% 的片，触发PLC的TCP的命中消息
            clearTimeout(timmer6);
        }, 12000)
    }
}

const cameraId = {}
let a = -1;
let b = -1;
let c = -1;
let d = -1;
let e = -1;
let f = -1;

new Main();